import {action, computed, observable} from 'mobx';
import {message} from 'antd';
import {WeaTableNew} from 'comsMobx';
import {accessAgentApi} from "../../apis/performance/accessAgentApi";
import {WeaLocaleProvider} from 'ecCom';
import {idsAndNamesToArr} from "../../util/Util";

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-考核代理
 * jiansong 2020/4/13
 */
const {TableStore} = WeaTableNew;

export default class AccessAgentStore {

    @observable loading = true; //加载中
    @observable myagnt = true; //加载中
    @observable userid = ""; //当前用户id
    @observable username = ""; //当前用户名称
    @observable tableStore = new TableStore();
    @observable listParams = {
        beagenterIds: "",//被代理人id
        beagenterNames: "",//被代理人id
        agenterIds: "",//代理人id
        agenterNames: "",//代理人id
        myagent: true,//是否查询我的代理
        status: '0',//代理状态
        programtype: '0',//代理类型
    }
    @observable showSearchAd = false; // 高级搜索面板显示

    @computed get beagentData() { //被代理人回显数据
        return idsAndNamesToArr(this.listParams.beagenterIds + '', this.listParams.beagenterNames);
    }

    @computed get agenterData() { //代理人回显数据
        return idsAndNamesToArr(this.listParams.agenterIds + '', this.listParams.agenterNames);
    }

    @action("重置查询条件")
    resetSearchAd = () => {
        if (this.listParams.myagent) {
            this.listParams = {
                ...this.listParams,
                agenterIds: "",//代理人id
                agenterNames: "",//代理人id
                status: '0',//代理状态
                programtype: '0',//代理类型
            };
        } else {
            this.listParams = {
                ...this.listParams,
                beagenterIds: "",//被代理人id
                beagenterNames: "",//被代理人id
                status: '0',//代理状态
                programtype: '0',//代理类型
            };
        }
    };

    @action("查询条件改变")
    onListParamsChange = (params) => {
        for (let key in params) {
            this.listParams[key] = params[key];
        }
    };

    @action("搜索")
    advanceSearch = () => {
        this.showSearchAd = false;
        this.getAgentListData();
    };

    @action("获取代理数据")
    getAgentListData = () => {
        let params = {
            operation: 'agentList',
            ...this.listParams
        }
        this.loading = true;
        accessAgentApi(params).then(action(res => {
            if (res.api_status) {
                this.userid = res.userid;
                this.username = res.username;
                if(this.listParams.myagent){
                    this.listParams.beagenterIds = res.userid;
                    this.listParams.beagenterNames = res.username;
                }else{
                    this.listParams.agenterIds = res.userid;
                    this.listParams.agenterNames = res.username;
                }
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'));
            }
            this.loading = false;
        }))
    };

    @action("编辑代理数据")
    doAgentData = (params) => {
        this.loading = true;
        accessAgentApi(params).then(action(res => {
            if (res.api_status) {
                message.success(getLabel('30700', '操作成功'));
                this.getAgentListData();
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'));
            }
            this.loading = false;
        }))
    };

}